package com.cloud.form.common.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class JsonHelper {

	public static void main(String[] args) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("AAA", "陶明龙");
		map.put("BBB", "童小胖");
		String a = replaceELValue("AAAAAAAAAA${AAA}AAAA${BBB}AAA", JSON.toJSONString(map));
		System.out.println(a);
	}

	/**
	 * 用于在jsonString中查找参数，替换sourceString中的EL表达式
	 * 
	 * @param sourceString
	 *            update form set xxx = '${xx.xx}' where xx = xx
	 * @param jsonString
	 *            xxx.xxx[0].xxx
	 * @return
	 */
	public static String replaceELValue(String sourceString, String jsonString) {
		Pattern pattern = Pattern.compile("\\$\\{(.*?)\\}");
		Matcher matcher = pattern.matcher(sourceString);

		JSONObject jsonObj = JSON.parseObject(jsonString);
		while (matcher.find()) {
			String fullText = matcher.group();
			String elContent = matcher.group(1);
			String[] propertys = elContent.trim().split(",");
			if (propertys.length == 0)
				throw new RuntimeException("解析EL表达式失败，请检查EL表达式配置");

			for (int i = 0; i < propertys.length; i++) {
				if(i + 1  == propertys.length){
					//直接替换为字符串
//					sourceString = sourceString.replace(fullText, value);
				}else{
					
				}
			}
			
		}
		return sourceString;
	}
}
